package com.blog.service.Impl;

import com.blog.common.ServerResponse;
import com.blog.dao.ArticleMapper;
import com.blog.dao.CategoryMapper;
import com.blog.pojo.Article;
import com.blog.pojo.Category;
import com.blog.service.IArticleService;
import com.blog.service.ICategoryService;
import com.sun.security.ntlm.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Blob;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @create 2018-06-07 18:32
 **/
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;


    /**
     * 查询所有正常状态的文章
     *
     * @return
     */
    @Override
    public List<Article> list(Map param) {

        return articleMapper.selectAllByAnimal(param);
    }

    /**
     * 查询单个文章信息
     *
     * @return
     */
    @Override
    public Article selectById(String id) {


        return articleMapper.selectById(id);
    }

    /**
     * 更新/新增文章
     *
     * @param article
     * @return
     */
    @Override
    public ServerResponse save(Article article) {
        //判断是更新还是新增
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = formatter.format(currentTime);
        int count = 0;
        if (StringUtils.isEmpty(article.getId())) {
            //新增
            article.setStatus(1);
            article.setUpdateTime(nowTime);
            article.setViewCount(1);

            count = articleMapper.insert(article);
            if (count == 0) {
                return ServerResponse.error("增加文章错误");
            }
        } else {

            article.setUpdateTime(nowTime);
            //修改
            count = articleMapper.update(article);
            if (count == 0) {
                return ServerResponse.error("更新文章错误");
            }

        }
        return ServerResponse.success();
    }

    /**
     * 批量更新状态
     * @param param
     */
    @Override
    public void bachUpdate(Map<String, Object> param) {
        articleMapper.bachUpdate(param);
    }

    /**
     * 批量彻底删除
     * @param param
     */
    @Override
    public void bachDelete(HashMap<String, Object> param) {
        articleMapper.bachDelete(param);
    }

    @Override
    public Blob test() {
        return  articleMapper.test();
    }

    /**
     * 更新文章状态
     * @param article
     */
    @Override
    public void update(Article article) {
        int count = articleMapper.update(article);
    }
}
